HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux ip-172-26-0-120 6.17.0-1009-aws #9~24.04.2-Ubuntu SMP Fri Mar 6 23:50:29 UTC 2026 x86_64
User: ubuntu (1000)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/html/dashboard.orbiwheels.com/vendor/cuyz/valinor/src/Mapper/Source/JsonSource.php
<?php

declare(strict_types=1);

namespace CuyZ\Valinor\Mapper\Source;

use CuyZ\Valinor\Mapper\Source\Exception\InvalidJson;
use CuyZ\Valinor\Mapper\Source\Exception\InvalidSource;
use CuyZ\Valinor\Mapper\Source\Exception\SourceNotIterable;
use Iterator;
use IteratorAggregate;
use JsonException;
use Traversable;

use function is_iterable;
use function json_decode;

use const JSON_THROW_ON_ERROR;

/**
 * @api
 *
 * @implements IteratorAggregate<mixed>
 */
final class JsonSource implements IteratorAggregate
{
    /** @var iterable<mixed> */
    private iterable $source;

    /**
     * @throws InvalidSource
     */
    public function __construct(string $jsonSource)
    {
        try {
            $source = json_decode($jsonSource, associative: true, flags: JSON_THROW_ON_ERROR);
        } catch (JsonException $e) {
            throw new InvalidJson($jsonSource, $e);
        }

        if (! is_iterable($source)) {
            throw new SourceNotIterable($jsonSource);
        }

        $this->source = $source;
    }

    /**
     * @return Iterator<mixed>
     */
    public function getIterator(): Traversable
    {
        yield from $this->source;
    }
}